﻿$(function () {
    var validate = new Validate();
    var url = new Url();
    var $Id = url.GetQueryString("id");
    var serializer = new JSONSerializer();
    var dict = new Dict();


    //绑定用户级别
    //var getUserGrade = dict.GetDictDDLByType(10);
    //$("#ddlUserGrade").html(getUserGrade);

    //绑定证件类型
    var getCardType = dict.GetDictDDLByType(16);
    $(".CertificateType").html(getCardType);

    //如果id存在，则获取并绑定用户信息
    if ($Id) {
        $("#formArea [name]").addClass("text").addClass("required").siblings("label").find("span").text("*");
        $("#formData [name]").addClass("text").siblings("label").find("span").text("*");
        $.getJSON("../Handler/FinancialUser.ashx", { Id: $Id, type: 4, _: Math.random() },
            function (result) {
                if (result.Status) {
                    serializer.DeSerialize($("#formArea"), result.Data);
                    // $(".username").val(result.Data.Name);
                    // $(".phone").val(result.Data.Phone);
                    $(".male")[0].checked = result.Data.Sex;
                    $(".female")[0].checked = !result.Data.Sex;
                    $(".hdUserId").val($Id);
                    if (result.Data.VistRecord && result.Data.VistRecord.length > 0) {
                        $(".txtVisit").val(result.Data.VistRecord[0].VisitMemo);
                    }
                }
            }
        );
    }




    //取得#formData相关表单元素
    var $CertificateType = $("#formArea select[name='CertificateType']");
    var $IDCard = $("#IDCard");
    var $Email = $("#formArea .Email");
    var $Address = $("#formArea .Address");
    var $AddressZipCode = $("#formArea .AddressZipCode");


    //保存
    $("#save_userinfo").bind("click", function () {
        var errorNode = null;
        $("#formArea .required ").each(function (index, item) {
            var value = $(this).val();
            if (value == "" || value == -1) {
                $name = $(this).attr("data-name");
                errorNode = $(this);
                $(this).next("span").text($name + "不能为空！");
                $(this).focus();
                return false;
            }
            else {
                if ($(this).attr("class").indexOf("verifyPhone") > 0) {
                    if (!validate.CheckPhone($(this).val())) {
                        errorNode = $(this);
                        $(this).next("span").text("无效的手机号码！");
                        $(this).focus();
                        return false;
                    }
                }
            }
        });
        if (errorNode) {
            return false;
        }
        if ($CertificateType.val() != "-1") {
            if ($IDCard.val() == "") {
                $IDCard.next("span").text($IDCard.attr("data-name") + "不能为空");
                return false;
            }
            var msg = new Array("验证通过", "非法身份证号", "非法地区", "非法生日");
            var i = 1;
            if ($CertificateType.val() == "485") {
                i = validate.CheckIdCard($IDCard.val());
            }
            if (i < 0) {
                $IDCard.next("span").text(msg[Math.abs(i)]);
                return;
            }
        }
        if ($IDCard.val()) {
            if ($CertificateType.val() == "-1") {
                $CertificateType.next("span").text($CertificateType.attr("data-name") + "不能为空");
                return false;
            }
        }

        if ($Email.val() != "") {
            if (!validate.CheckEmail($Email.val())) {
                $(".errorEmail").text("无效的电子邮箱");
                return false;
            }
        }
        if ($AddressZipCode.val() != "") {
            if (!validate.CheckAddressZipCode($AddressZipCode.val())) {
                $(".errorAddressZipCode").text("无效的邮政编码");
                return false;
            }
        }
        var $formData = serializer.Serialize($("#formArea"));
        $formData.Sex = $('input:radio[name="Gender"]:checked').val();
        if ($Id) {
            $formData.UserId = $Id;
            if ($IDCard.val() == "") {
                $CertificateType.next("span").text($CertificateType.attr("data-name") + "不能为空");
                $IDCard.next("span").text($IDCard.attr("data-name") + "不能为空");
                return false;
            }
            if ($Email.val() == "") {
                $Email.next("span").text($Email.attr("data-name") + "不能为空！");
                return false;
            }
            if ($Address.val() == "") {
                $Address.next("span").text($Address.attr("data-name") + "不能为空！");
                return false;
            }
        }
        $formData._ = Math.random();
        $formData.Type = $Id ? 2 : 1;
        $formData.Id = $Id;
        $formData.Sex = $(".male")[0].checked;
        $.getJSON(
       "../Handler/FinancialUser.ashx",
       $formData,
       function (result) {
           if (result.Status) {
               $(".hdUserId").val(result.Data)
               alert(result.Message);
               $(".progressBar").text("50%").css("width", "50%");
               $(".ul1 li:eq(2)").css({ "background": "#FF612C" }).click().siblings().css({ "background": "#4ABAE0" });
           }
           else {
               alert(result.Message);
           }
       }
   );
    });

    //添加回访信息
    $(".save_c2").bind("click", function () {
        var $userId = $(".hdUserId").val();
        if (!$userId) {
            alert("请先保存用户信息！");
            return;
        }
        var $visitInfo = $(".txtVisit").val();
        var $data = { UserId: $userId, VisitInfo: $visitInfo, Type: 1, _: Math.random() };//默认添加
        if ($userId != null) {//修改
            $data["Type"] = 2;
        }
        $.ajax({
            type: "post",
            url: "../Handler/VisitHandler.ashx",
            data: $data,
            dataType: "json",
            complete: function () {
            },
            async: true,
            cache: false,
            success: function (result) {
                if (result) {
                    $(".progressBar").css("width", "100%").text("100%");
                    window.location.href = "FinancialNewProj.aspx?css=1019";
                } else {
                    alert(result.Message);
                }
            }
        });
    });

    $(".username").change(function () {
        $(".errorName").text("");
    });

    $(".phone").change(function () {
        $(".errorPhone").text("");
    });

    //点击手续上传时绑定上传文件列表
    $("li:contains('紧急联系人资料录入')").click(function () {
        //编辑模式且列表为空时加载数据
        var $Id = $(".hdUserId").val();
        if (!$Id) {
            alert("请先完善个人信息！");
            setTimeout(function () {
                $("li:contains('个人信息')").click();
            }, 100);
        }
        else {
            $.getJSON(
            "../Handler/FinaUserEmgyHandler.ashx",
            { "UserId": $Id, "type": "0", _: Math.random() },
            function (result) {
                if (result.Status) {
                    serializer.DeSerialize($("#formData"), result.Data);
                    $(".male")[1].checked = result.Data.Sex;
                    $(".female")[1].checked = !result.Data.Sex;
                    $(".hdEmergyId").val(result.Data.EmergyId);
                }
                /*   else {
                      alert(result.Message);
                  }*/
            });
        }
    });

    $("li:contains('回访记录')").click(function () {
        //编辑模式且列表为空时加载数据
        var $Id = $(".hdUserId").val();
        if (!$Id) {
            alert("请先完善个人信息！");
            setTimeout(function () {
                $("li:contains('个人信息')").click();
            }, 100);
        }
        else {
            $.getJSON(
            "../Handler/VisitHandler.ashx",
            { "UserId": $Id, "type": "11" },
            function (result) {
                if (result != "") {
                    $(".txtVisit").val(result.VisitMemo)
                }
                /*   else {
                      alert(result.Message);
                  }*/
            });
        }
    });



    //添加紧急联系人
    $("#save_emgy").click(function () {
        var serializer = new JSONSerializer();
        var $formData = serializer.Serialize($("#formData")[0]);
        if ($Id) {
            var erroElement;//校验
            var $CertificateType = $("#formData  select.CertificateTypeEmgy");
            $("#formData .text").each(function (index, item) {
                var value = $(this).val();
                if ($(this).is("select")) {
                    if (value <= 0) {
                        erroElement = $(this);
                        return false;
                    }
                } else if (!value) {
                    erroElement = $(this);
                    return false;
                }
            });
            if (erroElement) {
                var name = erroElement.attr("data-name");
                erroElement.siblings(".error").text(name + "不能为空");
                erroElement.focus();
                return;
            }

            if (!validate.CheckPhone($formData.Phone)) {
                $(".errorPhoneEmgy").text('无效的手机号码');
                return;
            }
            if (!validate.CheckEmail($formData.Email)) {
                $(".errorEmailEmgy").text("无效的电子邮箱");
                return false;
            }
            var msg = new Array("验证通过", "非法身份证号", "非法地区", "非法生日");

            var i = 1;
            if ($(".CertificateTypeEmgy").val() == "485") {
                i = validate.CheckIdCard($formData.CertificateNo);
            }

            if (i < 0) {
                $(".errorIdEmgy").text(msg[Math.abs(i)]);
                return;
            }

        }
        var $emgyId = $(".hdEmergyId").val();
        $formData._ = Math.random();
        $formData.type = $emgyId ? 2 : 1;
        $formData.UserId = $(".hdUserId").val();
        $formData.Sex = $(".male")[1].checked;
        $formData.EmergyId = $emgyId;
        $.ajax({
            type: "post",
            url: "../Handler/FinaUserEmgyHandler.ashx",
            dataType: "json",
            data: $formData,
            async: true,
            cache: false,
            complete: function () {
            },
            success: function (result) {
                if (result.Status) {
                    alert(result.Message);
                    $(".progressBar").text("50%").css("width", "50%");
                    $(".ul1 li:eq(2)").css({ "background": "#FF612C" }).click().siblings().css({ "background": "#4ABAE0" });
                }
                else {
                    alert(result.Message);
                }
            }
        });
    });

    $("#formArea .required ").change(function () {
        var errorNode = null;
        var value = $(this).val();
        if (value == "" || value == -1) {
            $name = $(this).attr("data-name");
            errorNode = $(this);
            $(this).next("span").text($name + "不能为空！");
            $(this).focus();
            return false;
        }
        else {
            if ($(this).attr("class").indexOf("verifyPhone") > 0) {
                if (!validate.CheckPhone($(this).val())) {
                    errorNode = $(this);
                    $(this).next("span").text("无效的手机号码！");
                    $(this).focus();
                    return false;
                }
                else {
                    $(this).next("span").empty();
                }
            }
            else {
                $(this).next("span").empty();
            }
        }
        if (errorNode) {
            return;
        }
    });

    $CertificateType.change(function () {
        if ($CertificateType.val() != "-1") {
            if ($IDCard.val() == "") {
                $IDCard.next("span").text($IDCard.attr("data-name") + "不能为空");
                return false;
            }
            var msg = new Array("验证通过", "非法身份证号", "非法地区", "非法生日");
            var i = 1;
            if ($CertificateType.val() == "485") {
                i = validate.CheckIdCard($IDCard.val());
            }
            else {
                $CertificateType.next("span").empty();
                $IDCard.next("span").empty();
                return;
            }
            if (i < 0) {
                $CertificateType.next("span").empty();
                $IDCard.next("span").text(msg[Math.abs(i)]);
                return;
            }
        }
        else {
            if ($IDCard.val() != "") {
                $CertificateType.next("span").text($CertificateType.attr("data-name") + "不能为空");
                $IDCard.next("span").empty();
                return;
            }
            else {
                $CertificateType.next("span").empty();
                $IDCard.next("span").empty();
            }
        }
    });

    $IDCard.change(function () {
        if ($IDCard.val()) {
            var msg = new Array("验证通过", "非法身份证号", "非法地区", "非法生日");
            var i = 1;
            if ($CertificateType.val() == "485") {
                i = validate.CheckIdCard($IDCard.val());
            }
            if (i < 0) {
                $IDCard.next("span").text(msg[Math.abs(i)]);
                return;
            }
            else {
                $(this).next("span").empty();
            }
            if ($CertificateType.val() == "-1") {
                $CertificateType.next("span").text($CertificateType.attr("data-name") + "不能为空");
                return false;
            }
            else {
                $(this).next("span").empty();
            }
        }
        else {
            if ($CertificateType.val() != "-1") {
                $IDCard.next("span").text($IDCard.attr("data-name") + "不能为空");
            }
            else {
                $CertificateType.next("span").empty();
            }
        }
    });

    $Email.change(function () {
        if ($Email.val() != "") {
            if (!validate.CheckEmail($Email.val())) {
                $Email.next("span").text("无效的电子邮箱");
                return false;
            }
            else {
                $Email.next("span").empty();
            }
        }
        else {
            $Email.next("span").empty();
        }

    });




    //取得#formData相关表单元素
    var $CertificateTypeEmgy = $("#formData select[name='CertificateType']");
    var $IDCardEmgy = $("#formData .IDCard");
    var $EmailEmgy = $("#formData .Email");
    var $AddressEmgy = $("#formData .Address");

    $("#formData .text").change(function () {
        if ($Id) {
            var value = $(this).val();
            var name = $(this).attr("data-name");
            if ($(this).is("select")) {
                if (value <= 0) {
                    $(this).siblings(".error").text(name + "不能为空");
                    $(this).focus();
                    return false;
                }
            } else if (!value) {
                $(this).siblings(".error").text(name + "不能为空");
                $(this).focus();
                return false;
            }
        }
        if ($(this).attr("class").indexOf("verifyPhone") > 0) {
            if (!validate.CheckPhone($(this).val())) {
                errorNode = $(this);
                $(this).next("span").text("无效的手机号码！");
                $(this).focus();
                return false;
            }
            else {
                $(this).next("span").empty();
            }
        }
    });

    $CertificateTypeEmgy.change(function () {
        if ($CertificateTypeEmgy.val() != "-1") {
            if ($IDCardEmgy.val() == "") {
                $IDCardEmgy.next("span").text($IDCardEmgy.attr("data-name") + "不能为空");
                return false;
            }
            var msg = new Array("验证通过", "非法身份证号", "非法地区", "非法生日");
            var i = 1;
            if ($CertificateTypeEmgy.val() == "485") {
                i = validate.CheckIdCard($IDCardEmgy.val());
            }
            else {
                $CertificateTypeEmgy.next("span").empty();
                $IDCardEmgy.next("span").empty();
                return;
            }
            if (i < 0) {
                $CertificateTypeEmgy.next("span").empty();
                $IDCardEmgy.next("span").text(msg[Math.abs(i)]);
                return;
            }
        }
        else {
            if ($IDCardEmgy.val() != "") {
                $CertificateTypeEmgy.next("span").text($CertificateTypeEmgy.attr("data-name") + "不能为空");
                $IDCardEmgy.next("span").empty();
                return;
            }
            else {
                $CertificateType.next("span").empty();
                $IDCardEmgy.next("span").empty();
            }
        }
    });

    $IDCardEmgy.change(function () {
        if ($IDCardEmgy.val()) {
            var msg = new Array("验证通过", "非法身份证号", "非法地区", "非法生日");
            var i = 1;
            if ($CertificateTypeEmgy.val() == "485") {
                i = validate.CheckIdCard($IDCardEmgy.val());
            }
            if (i < 0) {
                $IDCardEmgy.next("span").text(msg[Math.abs(i)]);
                return;
            }
            else {
                $(this).next("span").empty();
            }
            if ($CertificateTypeEmgy.val() == "-1") {
                $CertificateTypeEmgy.next("span").text($CertificateTypeEmgy.attr("data-name") + "不能为空");
                return false;
            }
            else {
                $(this).next("span").empty();
            }
        }
        else {
            if ($CertificateTypeEmgy.val() != "-1") {
                $IDCardEmgy.next("span").text($IDCardEmgy.attr("data-name") + "不能为空");
            }
            else {
                $CertificateTypeEmgy.next("span").empty();
            }
        }
    });

    $EmailEmgy.change(function () {
        if ($EmailEmgy.val() != "") {
            if (!validate.CheckEmail($EmailEmgy.val())) {
                $EmailEmgy.next("span").text("无效的电子邮箱");
                return false;
            }
            else {
                $EmailEmgy.next("span").empty();
            }
        }
        else {
            $EmailEmgy.next("span").empty();
        }

    });

    //身份证查询是否已经关单
    $(".btnCorrelationOrder").click(function () {
        if ($("#IDCard").val().length == 0) {
            $("#IDCard").next("span").text($("#IDCard").attr("data-name") + "不能为空");
            return false;
        }
        $.ajax({
            url: "../Handler/FinancialOrder.ashx",
            type: "post",
            dataType: "json",
            data: { IDCard: $("#IDCard").val(), Type: 26, _: Math.random() },
            async: true,
            cache: true,
            complete: function () { },
            success: function (result) {
                alert(result.Message);
            }
        });


    });




})



